<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16"></script>
</head>
<body>
    <div id="app" style="margin: 100px;">
    <parent></parent>
    </div>

    <template id="par">
        <div>
            <h1>{{parentMsg}}:{{message}}</h1>
            <child1 @change="getVal"></child1>
        </div>
    </template>

    <template id="c1">
        <div>
            <button @click="fn">
                点击传值父组件
            </button>
            <p>准备传递的数据:{{cMsg1}}</p>
        </div>
    </template>
</body>
<script>
    var vm = new Vue({
        el:"#app",
        data:{

        },
        components:{
            "parent":{
                template:"#par",
                data(){
                    return {
                        parentMsg:"父组件接收的信息",
                        message:""
                    }
                },
                methods:{
                    getVal:function(val){
                        this.message=val;
                        console.log("接收的信息为:"+this.message);
                    }
                },
                components:{
                    "child1":{
                        template:"#c1",
                        data(){
                            return{
                                cMsg1:"1000"
                            }
                        },
                        methods:{
                            fn:function(){
                                this.$emit("change",this.cMsg1)
                            }
                        }
                    }
                }
            }
        }
    })
</script>
</html>